<?php
	function _session_start(){
		if(mt_rand(1,1)==1){
			_session_gc();//执行垃圾回收
		}
		$sessionid  =isset($_COOKIE['hdsessid'])?$_COOKIE['hdsessid']: time().mt_rand(1,1000);
		$GLOBALS['sessionid'] = $sessionid;
		if(is_file("session/".$sessionid)){
			$GLOBALS['SESSION'] =  unserialize(file_get_contents("session/".$sessionid));
		}else{
			$GLOBALS['SESSION'] = array();
		}
		//如果客户端有$_COOKIE['hdsessid']就不设置了
		isset($_COOKIE['hdsessid']) || setcookie("hdsessid",$sessionid,0,'/');
	}
	//写入SESSION
	function _session_write(){
		file_put_contents("session/".$GLOBALS['sessionid'],serialize($GLOBALS['SESSION']));
	}
	//垃圾回收GC
	function _session_gc(){
		$_maxlifetime = 3;//过期时间
		foreach(glob("session/*")  as $file){
			if(isset($GLOBALS['sessionid']) && $file==$GLOBALS['sessionid'])continue;//如果文件为当前操作用户时，不进行删除操作
			if(filemtime($file)+$_maxlifetime<time()){
				@unlink($file);
			}
		} 
	}
?>